<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchFormData" class="demo-form-inline" size="mini">
            	            	        <el-form-item
      	        label="用户ID"
              >
      	<el-input v-model="searchFormData.adminUserId" :clearable="true" placeholder="用户ID" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="用户用户名（冗余）"
              >
      	<el-input v-model="searchFormData.username" :clearable="true" placeholder="用户用户名（冗余）" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="登录IP地址"
              >
      	<el-input v-model="searchFormData.ip" :clearable="true" placeholder="登录IP地址" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="浏览器内核"
              >
      	<el-input v-model="searchFormData.userAgent" :clearable="true" placeholder="浏览器内核" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="登录时间"
              >
      	<el-input v-model="searchFormData.loginTime" :clearable="true" placeholder="登录时间" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="数据创建时间"
              >
      	<el-input v-model="searchFormData.createTime" :clearable="true" placeholder="数据创建时间" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="数据最后修改时间"
              >
      	<el-input v-model="searchFormData.updateTime" :clearable="true" placeholder="数据最后修改时间" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="数据创建者"
              >
      	<el-input v-model="searchFormData.creator" :clearable="true" placeholder="数据创建者" style="width: 250px;" />
      </el-form-item>
                    	        <el-form-item
      	        label="数据更新者"
              >
      	<el-input v-model="searchFormData.updater" :clearable="true" placeholder="数据更新者" style="width: 250px;" />
      </el-form-item>
                    <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="loadTable">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" size="mini" icon="el-icon-plus" style="margin-bottom: 10px;" @click="onAdd">新增</el-button>
    <el-table
      :data="pageInfo.rows"
      border
      highlight-current-row
    >
          <el-table-column
        prop="id"
                label="id"
              />
           <el-table-column
        prop="adminUserId"
                label="用户ID"
              />
           <el-table-column
        prop="username"
                label="用户用户名（冗余）"
              />
           <el-table-column
        prop="ip"
                label="登录IP地址"
              />
           <el-table-column
        prop="userAgent"
                label="浏览器内核"
              />
           <el-table-column
        prop="loginTime"
                label="登录时间"
              />
           <el-table-column
        prop="createTime"
                label="数据创建时间"
              />
           <el-table-column
        prop="updateTime"
                label="数据最后修改时间"
              />
           <el-table-column
        prop="creator"
                label="数据创建者"
              />
           <el-table-column
        prop="updater"
                label="数据更新者"
              />
          <el-table-column>
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="onTableUpdate(scope.row)">修改</el-button>
          <el-button type="text" size="mini" @click="onTableDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      style="margin-top: 5px"
      :current-page="searchFormData.pageIndex"
      :page-size="searchFormData.pageSize"
      :page-sizes="[5, 10, 20, 40]"
      :total="pageInfo.total"
      layout="total, sizes, prev, pager, next"
      @size-change="onSizeChange"
      @current-change="onPageIndexChange"
    />
    <!--dialog-->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      @close="resetForm('dialogForm')"
    >
      <el-form
        ref="dialogForm"
        :rules="dialogFormRules"
        :model="dialogFormData"
        label-width="120px"
        size="mini"
      >
            	            	        <el-form-item
      	prop="adminUserId"
                label="用户ID"
               >
          <el-input v-model="dialogFormData.adminUserId" />
        </el-form-item>
                    	        <el-form-item
      	prop="username"
                label="用户用户名（冗余）"
               >
          <el-input v-model="dialogFormData.username" />
        </el-form-item>
                    	        <el-form-item
      	prop="ip"
                label="登录IP地址"
               >
          <el-input v-model="dialogFormData.ip" />
        </el-form-item>
                    	        <el-form-item
      	prop="userAgent"
                label="浏览器内核"
               >
          <el-input v-model="dialogFormData.userAgent" />
        </el-form-item>
                    	        <el-form-item
      	prop="loginTime"
                label="登录时间"
               >
          <el-input v-model="dialogFormData.loginTime" />
        </el-form-item>
                    	        <el-form-item
      	prop="createTime"
                label="数据创建时间"
               >
          <el-input v-model="dialogFormData.createTime" />
        </el-form-item>
                    	        <el-form-item
      	prop="updateTime"
                label="数据最后修改时间"
               >
          <el-input v-model="dialogFormData.updateTime" />
        </el-form-item>
                    	        <el-form-item
      	prop="creator"
                label="数据创建者"
               >
          <el-input v-model="dialogFormData.creator" />
        </el-form-item>
                    	        <el-form-item
      	prop="updater"
                label="数据更新者"
               >
          <el-input v-model="dialogFormData.updater" />
        </el-form-item>
                    </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onDialogSave">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
// 创建axios实例
const client = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
  timeout: 60000 // 请求超时时间,60秒
})
export default {
  data() {    
    return {
      searchFormData: {
                id: '',
                adminUserId: '',
                username: '',
                ip: '',
                userAgent: '',
                loginTime: '',
                createTime: '',
                updateTime: '',
                creator: '',
                updater: '',
                pageIndex: 1,
        pageSize: 10
      },
      pageInfo: {
        rows: [],
        total: 0
      },
      dialogVisible: false,
      dialogTitle: '',
      dialogFormData: {
                 id: ''
                , adminUserId: ''
                , username: ''
                , ip: ''
                , userAgent: ''
                , loginTime: ''
                , createTime: ''
                , updateTime: ''
                , creator: ''
                , updater: ''
              },
      dialogFormRules: {        
                 id: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , adminUserId: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , username: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , ip: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , userAgent: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , loginTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , createTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , updateTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , creator: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
                , updater: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
              }
    }
  },
  created() {
    this.loadTable()
  },
  methods: {
    loadTable() {
      this.post('/adminUserLoginLog/list', this.searchFormData, resp => {
        this.pageInfo.rows = resp.data
      })
    },
    onTableUpdate(row) {
      this.dialogTitle = '修改'
      this.dialogVisible = true
      this.$nextTick(() => {
        Object.assign(this.dialogFormData, row)
      })
    },
    onTableDelete(row) {
      this.confirm(`确认要删除该记录吗？`, function(done) {
        const data = {
          id: row.id
        }
        this.post('/adminUserLoginLog/del', data, () => {
          done()
          this.tip('删除成功')
          this.loadTable()
        })
      })
    },
    onDialogSave() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          const uri = this.dialogFormData.id ? '/adminUserLoginLog/update' : '/adminUserLoginLog/add'
          this.post(uri, this.dialogFormData, () => {
            this.dialogVisible = false
            this.loadTable()
          })
        }
      })
    },
    onSizeChange(size) {
      this.searchFormData.pageSize = size
      this.loadTable()
    },
    onAdd() {
      this.dialogTitle = '新增'
      this.dialogVisible = true
      this.dialogFormData.id = 0
    },
    onPageIndexChange(pageIndex) {
      this.searchFormData.pageIndex = pageIndex
      this.loadTable()
    },
    /**
     * 请求接口
     * @param uri uri
     * @param data 请求数据
     * @param callback 成功时回调
     * @param errorCallback 错误时回调
     */
    post(uri, data, callback, errorCallback) {
      const that = this
      client.post(uri, data).then(function(response) {
        const resp = response.data
        const code = resp.code
        if (code === '0') { // 成功
          callback && callback.call(that, resp)
        } else {
          that.$message.error(resp.msg)
        }
      }).catch(function(error) {
        console.error(error)
        errorCallback && errorCallback(error)
        that.$message.error(error.message)
      })
    }
  }
}
</script>